package response;

import core.GameUser;
import core.GamePacket;

/**
 * Response update class mostly created by heartbeat request and
 * sometimes by other requests.
 *
 * @author eric, yiyi
 *
 */
public class ResponseUpdate extends GameResponse {

    // The update requester
    private GameUser user;

    @Override
    public void run() {
        synchronized (user) {
            try {
                // Get all update responses for this user and
                // send 'em out.

                /* TODO implement
                for (GameResponse res : user.getSpecialUpdateResponses()) {
                    res.setOut(out);
                    res.run();
                }

                user.clearSpecialUpdateResponses();
                */

                // Send number of responses ... then send responses?
                for (GameResponse response : user.getUpdateResponses()) {
                    response.setOut(output);
                    response.run();
                }
                user.clearUpdateResponses();

            } catch (Exception e) {
                gameServer.log(e.getMessage(), "e");
            }
        }
    }

    /**
     * Make sure this is the requester of this updating
     *
     * @param user      update requester
     */
    public void setUser(GameUser user) {
        this.user = user;
    }
    
    @Override
    public byte[] createResponse() {
        GamePacket packet = new GamePacket(code);
        return packet.getBytes();
    }
}